//
//  HBQSuperMarketViewController.m
//  HelloBeeQuick
//
//  Created by LiYiLin on 16/8/18.
//  Copyright © 2016年 LiYiLin. All rights reserved.
//

#import "HBQSuperMarketViewController.h"

#import "HBQsmListTableView.h"
#import "HBQsmContentTableView.h"
#import "HBQsmListModel.h"
#import "HBQsmContentModel.h"

@interface HBQSuperMarketViewController ()<UITableViewDelegate>

@property (nonatomic,strong)HBQsmListTableView*listTableView;

@property (nonatomic,strong)HBQsmContentTableView*contentTableView;


@property (nonatomic,strong)NSArray *listArray;
@property (nonatomic,strong)NSArray *contentArray;


@end

@implementation HBQSuperMarketViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
    [self getLoadData];
    
    
    
}
#pragma mark
#pragma mark - 懒加载

-(HBQsmListTableView *)listTableView{
    
    if (_listTableView == nil) {
        _listTableView = [[HBQsmListTableView alloc]initWithFrame:CGRectZero];
    }
    
    return _listTableView;
    
}

-(HBQsmContentTableView *)contentTableView{
    if (_contentTableView == nil) {
        _contentTableView = [[HBQsmContentTableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
        
        
    }
    return _contentTableView;
}


-(NSArray *)listArray{
    if (_listArray == nil) {
        _listArray = [NSArray array];
    }
    return _listArray;
}


-(NSArray *)contentArray{
    
    if (_contentArray == nil) {
        _contentArray = [NSArray array];
    }
    return _contentArray;
}


#pragma mark
#pragma mark - 设置UI

-(void)setupUI{
    
    //添加到主视图
    [self.view addSubview:self.listTableView];
    [self.view addSubview:self.contentTableView];
    
    //设置约束
    
    [self.listTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.view);
        make.bottom.equalTo(self.view);
        make.left.equalTo(self.view);
        make.width.mas_equalTo(KWIDTH * 0.3);
    }];
    
    [self.contentTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.listTableView.mas_top).offset(64);
        make.right.bottom.equalTo(self.view);
        make.left.equalTo(self.listTableView.mas_right);
    }];
    
    
    //设置内容行高
    self.contentTableView.rowHeight = 70;
    
    
    //设置代理
    
    self.listTableView.delegate = self;
    self.contentTableView.delegate = self;
    
}


#pragma mark
#pragma mark - 获取接口数据

-(void)getLoadData{
    
    NSString *urlString = @"http://iosapi.itcast.cn/loveBeen/supermarket.json.php";
    
    NSDictionary *paramaters = @{@"call": @5};
//    [SVProgressHUD show];
    
    [SVProgressHUD showWithStatus:@"加载中..."];
    [SVProgressHUD setDefaultStyle:SVProgressHUDStyleCustom];
    [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeCustom];

    [SVProgressHUD setBackgroundLayerColor:[UIColor whiteColor]];
    [[HBQNetWorkTools SharedManager]post_RequestWithUrl:urlString parameters:paramaters successBlock:^(id responseObjects) {
        
        
        
        
        NSDictionary *dict = responseObjects;
        NSDictionary *data = dict[@"data"];
        NSArray *categories = data[@"categories"];

        self.listArray = categories;
        
//        self.listTableView.listArray = categories;
        
        NSDictionary *products = data[@"products"];
        
        
        NSMutableArray *tempArray = [NSMutableArray array];
        
        NSMutableArray <HBQsmListModel*>*listArray = [NSMutableArray array];
        for (NSDictionary *dict in categories) {
            
            HBQsmListModel *model = [HBQsmListModel listModelWithDict:dict];
            
            
            [listArray addObject:model];
            
            NSString *key = model.contentId;
            
            NSArray *dataArray = products[key];

            [tempArray addObject:dataArray];
            
        }
        //列表数据
        self.contentArray = tempArray;
        
        
        
        
        NSMutableArray<NSArray<HBQsmContentModel*> *> *dataArray = [NSMutableArray array];
        [tempArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            NSMutableArray<HBQsmContentModel*> *muArray = [NSMutableArray array];
            for (NSDictionary *dict in obj) {
                HBQsmContentModel *contentModel = [HBQsmContentModel contentModelWithDict:dict];
                [muArray addObject:contentModel];
                
                
            }
            
            [dataArray addObject:muArray];
            
        }];
        

        self.listTableView.listArray = listArray;
        
        //内容数据
        self.contentTableView.dataArray = dataArray;
        
        self.contentTableView.listArray = listArray;
        
        

        [SVProgressHUD dismiss];
        
        
    } failureBlock:^(NSError *error) {
        
    }];
    
    
}



-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    if (tableView == self.listTableView) {
        
        
        
        UITableViewCell *cell =  [tableView cellForRowAtIndexPath:indexPath];
        
//        NSLog(@"%@",NSStringFromCGRect(cell.frame));
        
        if (cell.frame.origin.y > KHEIGHT * 0.5) {
            
            CGFloat y = cell.frame.origin.y - KHEIGHT * 0.5;
            
            NSLog(@"%lf",y);
            
            if (y >  100) {
                
                [tableView setContentOffset:CGPointMake(0, 100) animated:YES];
            }else{
                [tableView setContentOffset:CGPointMake(0, y) animated:YES];
            }
            
            
        }else{
            
            [tableView setContentOffset:CGPointMake(0, -64) animated:YES];
        }
        
        
        //使内容列表滚动到合适位置
        NSIndexPath *indexpath = [NSIndexPath indexPathForRow:0 inSection:indexPath.row];
        [self.contentTableView scrollToRowAtIndexPath:indexpath atScrollPosition:UITableViewScrollPositionTop animated:YES];
        
        
        
        
        
    }
    
}




@end

























